package com.xln.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xln.pojo.Tag;
import com.xln.service.TagService;
import com.xln.util.ResEnum;
import com.xln.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Slf4j
public class TagController {

    @Resource
    public TagService tagService;

//    查询 标签列表（全量或者关键字）
    @GetMapping("/tagList")
    public String tagList(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Model model
    ){
        Page<Tag> tagPage = new Page<>(pageNum, pageSize);

        if (keyword != null){

            Page<Tag> dbTagPage = tagService.page(tagPage,
                    Wrappers.<Tag>lambdaQuery()
                            .like(Tag::getName, keyword)
                            .or()
                            .like(Tag::getDescription, keyword)
                            .orderByDesc(Tag::getCreateTime)
            );
            log.info("标签列表：当前页={}, 总页数={}, 总记录数={}",
                    dbTagPage.getCurrent(), dbTagPage.getPages(), dbTagPage.getTotal());
            model.addAttribute( "tagPage", dbTagPage);

        }else {
            Page<Tag> dbTagPage = tagService.page(tagPage);
            log.info("标签列表：当前页={}, 总页数={}, 总记录数={}",
                    dbTagPage.getCurrent(), dbTagPage.getPages(), dbTagPage.getTotal());
            model.addAttribute( "tagPage", dbTagPage);
        }

        return "tag";
    }

//    查询 标签列表(ajax)
    @ResponseBody
    @GetMapping("/tagsAjax")
    public Result<List<Tag>> tagsPageAjax() {

        // 获取标签列表
        List<Tag> tags = tagService.list();

        return Result.success(ResEnum.SUCCESS, tags);
    }

//    保存标签
    @PostMapping("/tag/save")
    @ResponseBody
    public ResEnum tagSave(Tag formData, Model model){
        boolean result = tagService.save(formData);
        log.info("保存标签结果：{}", result);
        if (result){
            return ResEnum.SUCCESS;
        }else {
            return ResEnum.ERROR;
        }
    }

//    更新标签数据
    @PostMapping("/tag/update/{id}")
    @ResponseBody
    public ResEnum tagUpdate(
            @PathVariable Long id,
            Tag formData,
            Model model){
        formData.setId(id);
        boolean result = tagService.updateById(formData);
        log.info("更新标签结果：{}", result);
        if (result){
            return ResEnum.SUCCESS;
        }else {
            return ResEnum.ERROR;
        }
    }

//    删除标签
    @PostMapping("/tag/delete/{id}")
    @ResponseBody
    public ResEnum tagDelete(@PathVariable Long id, Model model){
        boolean result = tagService.removeById(id);
        log.info("删除标签结果：{}", result);
        if (result){
            return ResEnum.SUCCESS;
        }else {
            return ResEnum.ERROR;
        }
    }


}
